// app.js
import Ajax from "utils/ajax.js";
App({
  onLaunch() {
    // 展示本地存储能力
    var that = this;
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.removeStorageSync('token')

    //重力球
    wx.getSystemInfo({
      success(res) {
        that.globalData.ww = res.windowWidth;
        that.globalData.hh = res.windowHeight;
      }
    })

    // 登录
    wx.login({
      success: res => {
        var data = {
          "code": res.code
        }
        var ajax = new Ajax({
          data,
          path: '/api/login'
        })
        ajax.then(res => {
          if (!res.errors) {
            wx.removeStorageSync("token");
            wx.setStorageSync("token", res.data.token)
            //cb && cb()
            //this.getUserInfo()
          }

        })
        ajax.catch(err => {
          console.log(err)
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        //wx.removeStorageSync("token");
            //wx.setStorageSync("token", res.data.token)
      }
    })
  },
  globalData: {
    userInfo: null,
    publicAdress:'https://lake.zhimibook.com/',
    token: wx.getStorageSync("token"),
  }
})
